package com.bai.edu.service.impl;

import com.bai.base.exceptionHandler.BaiException;
import com.bai.edu.entity.Course;
import com.bai.edu.entity.CourseDescription;
import com.bai.edu.entity.Teacher;
import com.bai.edu.entity.frontvo.CourseFrontVo;
import com.bai.edu.entity.vo.CourseInfoVo;
import com.bai.edu.entity.vo.CoursePublishVo;
import com.bai.edu.entity.vo.CourseWebVo;
import com.bai.edu.mapper.CourseMapper;
import com.bai.edu.service.ChapterService;
import com.bai.edu.service.CourseDescriptionService;
import com.bai.edu.service.CourseService;
import com.bai.edu.service.VideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author bai
 * @since 2023-07-25
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private CourseDescriptionService courseDescriptionService;
    @Autowired
    private VideoService videoService;
    @Autowired
    private ChapterService chapterService;
    @Autowired
    private CourseMapper courseMapper;

    //添加课程基本信息
    @Override
    public void saveCourseInfo(CourseInfoVo courseInfoVo) {
        Course course = new Course();
        BeanUtils.copyProperties(courseInfoVo, course);
        int insert = baseMapper.insert(course);
        if (insert <= 0) {
            throw new BaiException(2001, "添加课程失败");
        }
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        courseDescriptionService.save(courseDescription);
    }

    //根据id查询课程信息
    @Override
    public CourseInfoVo getCourseInfoById(String courseId) {
        Course course = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(course,courseInfoVo);
        CourseDescription courseDescription = courseDescriptionService.getById(course);
        courseInfoVo.setDescription(courseDescription.getDescription());
        return courseInfoVo;
    }

    //修改课程信息
    @Override
    public void updateCourseInfoVo(CourseInfoVo courseInfoVo) {
        Course course = new Course();
        BeanUtils.copyProperties(courseInfoVo,course);
        int i = baseMapper.updateById(course);
        if (i==0){
            throw  new BaiException(20011,"修改失败");
        }
        CourseDescription courseDescription = new CourseDescription();
        BeanUtils.copyProperties(courseInfoVo,courseDescription);
        courseDescriptionService.updateById(courseDescription);
    }

    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
        return publishCourseInfo;
    }

    @Override
    public void removeCourse(String courseId) {
        videoService.removeVideoByCourseId(courseId);
        chapterService.removeChaptByCourseId(courseId);
        courseDescriptionService.removeById(courseId);
        int i = baseMapper.deleteById(courseId);
        if (i==0){
            throw new BaiException(20001,"删除失败");
        }
    }

    //条件查询带分页查询
    @Override
    public Map<String, Object> getCourseFrontList(Page<Course> coursePage, CourseFrontVo courseFrontVo) {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){//一级分类
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
            if (!StringUtils.isEmpty(courseFrontVo.getSubjectId())){//二级分类
                wrapper.eq("subject_id",courseFrontVo.getSubjectId());
            }

        }
        if (!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())){
            wrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())){
            wrapper.orderByDesc("gmt_create");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())){
            wrapper.orderByDesc("price");
        }
        baseMapper.selectPage(coursePage,wrapper);
        List<Course> records = coursePage.getRecords();
        long total = coursePage.getTotal();
        long size = coursePage.getSize();
        long current = coursePage.getCurrent();
        long pages = coursePage.getPages();
        boolean hasNext =  coursePage.hasNext();
        boolean hasPrevious = coursePage.hasPrevious();
        Map<String, Object> map = new HashMap<>();
        map.put("records",records);
        map.put("total",total);
        map.put("size",size);
        map.put("current",current);
        map.put("pages",pages);
        map.put("hasNext",hasNext);
        map.put("hasPreVious",hasPrevious);
        return map;
    }

    //根据课程id查询课程所有信息
    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        CourseWebVo courseWebVo = courseMapper.getBaseCourseInfo(courseId);
        return courseWebVo;
    }


}
